import 'dart:io';
import 'package:ali_vod_video/ali_vod_video.dart';
import 'package:ali_vod_video/upload_callback.dart';
import 'package:ali_vod_video_example/upload_video/video_play.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:images_picker/images_picker.dart';
// import 'package:image_picker/image_picker.dart';
// import 'package:matisse_android_plugin/export_matisse.dart';

class UploadVideoPage extends StatefulWidget {
  static const ROUTER = 'UploadVideoPage';

  @override
  State<StatefulWidget> createState() {
    return UploadVideoState();
  }

  UploadVideoPage();
}

class UploadVideoState extends State<UploadVideoPage> {
  double? _progress;
  double? _progress2;
  TextEditingController? _controller;

  String? _path;
  AliVodVideo? _aliVodVideo;
  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    _controller?.addListener(() {
      setState(() {});
    });
    _controller?.text = "1";
    _aliVodVideo = new AliVodVideo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                CupertinoButton(
                    child: Text(
                      "选择视频",
                    ),
                    onPressed: () {
                      _chooseVideoAndUpload();
                    }),
                CupertinoButton(
                    child: Text(
                      "停止",
                    ),
                    onPressed: () {
                      _cancelVideoUpload();
                    }),
              ]),
        ),
      ),
    );
  }

  void _chooseVideoAndUpload() async {
    List<Media>? resultList = await ImagesPicker.pick(
      count: 1,
      pickType: PickType.video,
    );
    if (resultList?.length == 0) {
      return;
    }
    _path = resultList?[0].path;

    _aliVodVideo?.registerNativeListener(new OnNativeMethodListener((progress){
      print("progress:" + progress.toString());
    }));
    String userData = '{"MessageCallback":{"CallbackURL":"http://et.lemontry.com/aliyunvod/transcodingCallback","CallbackType":"http"}, "Extend":"01"}';
    _aliVodVideo?.uploadVideo("123", "", "38259109e7108bc6586691f49a217c0d", "HD,SD", resultList?[0].path??'', userData, UploadCallback(
        onSuccess: (url, size, coverUrl,videoId, sdVideo, sdStorage) {
          Navigator.of(context).push(MaterialPageRoute(builder: (_) {
            return VideoPage(
              url: url,
            );
          }));
        },
        onError: (msg) {
          print(msg);
        },
      onCancel: (id, path){
        print("onCancel:-------------");
      }, onProgress: (double value, String id, {String? msg}) {

    },
    ));
  }

  void _cancelVideoUpload(){
    _aliVodVideo?.cancelUploadVideo("123", _path??'');
  }

  // void uploadVideoWithProgressTest(int quality) async {
  //   File file = await ImagePicker.pickVideo(source: ImageSource.gallery);
  // }
}
